/**
 * 初始化录量管理详情对话框
 */
var BizStudentinfoInfoDlg = {
    bizStudentinfoInfoData : {},
    validateFields: {
        studentName: {
            trigger: 'blur',
            validators: {
                notEmpty: {
                    message: '学生姓名不能为空'
                }
            }
        },
        studentPhone: {
            trigger: 'blur',
            validators: {
                notEmpty: {
                    message: '联系电话不能为空'
                },
                regexp: {
                    regexp: /^\d{7,11}$/,
                    message: '电话号码长度7位或11位'
                },
                remote: {
                    url: 'validate_1',
                    //delay: 1000,
                    data:function(validator) {
                        return {
                            "studentName": $("#studentName").val()
                        };
                    },
                    message: '该记录已被注册！<br/>录入7位号码时请检查号码+姓名组合的重复性!'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
BizStudentinfoInfoDlg.clearData = function() {
    this.bizStudentinfoInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
BizStudentinfoInfoDlg.set = function(key, val) {
    this.bizStudentinfoInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
BizStudentinfoInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
BizStudentinfoInfoDlg.close = function() {
    parent.layer.close(window.parent.BizStudentinfo.layerIndex);
}

/**
 * 收集数据
 */
BizStudentinfoInfoDlg.collectData = function() {
    this
    .set('id')
    .set('studentName')
    .set('studentPhone')
    .set('weixin')
    .set('qq')
    .set('studentSex')
    .set('studentEdu')
    .set('studentOldSchool')
    .set('isValid')
    .set('validDateTime')
    .set('createDateTime')
    .set('ownerID')
    .set('isDist')
    .set('sourceDateTime')
    .set('expirationTime')
    .set('remark')
    .set('distUserID');
}

/**
 * 验证数据是否为空
 */
BizStudentinfoInfoDlg.validate = function () {
    bv = $('#bizStudentinfoForm').data("bootstrapValidator");
    rs = bv.validate();
    flds = ["studentName","studentPhone"];
    // 验证所有表单输入项内容
    for(i in flds){
        for (validatorName in bv.options.fields[flds[i]].validators) {
            status = $(rs.getFieldElements(flds[i])).data('bv.result.' + validatorName);
            console.log(status);
            if(status == "INVALID"){
                return false;
            }
        }
    }
    return true;
};

/**
 * 提交添加
 */
BizStudentinfoInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/bizStudentinfo/add", function(data){
        if(data.code == 200) {
            Feng.success("添加成功!");
            BizStudentinfo.table.refresh();
        }else{
            Feng.error("添加失败！" + data.message + "!");
        }
        window.parent.BizStudentinfo.table.refresh();
        BizStudentinfoInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.bizStudentinfoInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
BizStudentinfoInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/bizStudentinfo/update", function(data){
        Feng.success("修改成功!");
        window.parent.BizStudentinfo.table.refresh();
        BizStudentinfoInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.bizStudentinfoInfoData);
    ajax.start();
}

$(function() {
    // 使用Bootstrap validator验证表单字段
    $('#bizStudentinfoForm').bootstrapValidator({
        feedbackIcons: {    //根据验证结果显示的各种图标
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: BizStudentinfoInfoDlg.validateFields,
        live: 'disable',
        message: '该字段不能为空'
    });
});
